/*
 * Dynamic Slider 0.1 - jQuery plugin
 * written by eighty4	
 * http://broken8.se/dynamic-slider
 *
 * Dual licensed under the MIT 
 * and GPL licenses.
 *
 * Heavily based on:
 *
 * * * Easy Slider 1.7 - jQuery plugin
 * * * written by Alen Grakalic	
 * * * http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 * * *
 * * * Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 * * * Dual licensed under the MIT
 * * * and GPL licenses.
 *
 * Usage:
 *
 * Initialize the script.
 ***********************************
   
 $(document).ready(function() {
 	$( '#slider ul' ).dynSlide({});
 });
 
 *
 * Markup example
 ***********************************
 
 <div id="slider">
	 <ul>
 		<li>1</li>
 		<li>2</li>
 		<li>3</li>
 		<li>4</li>
 		<li>5</li>
 	</ul>
 </div>
 
 *	
 * The standard settings bind "slide right" to the id #prev
 * and "slide left" to the id #next. If you want to change this
 * you can by passing { nextId: 'newid', prevId: 'newid2' }
 * to dynSlide().
 * There's a couple of more settings but I'm to lazy to describe
 * them.
 *
 * Don't try to get it working in ie6 or ie7, it won't work. 
 * All it does is 
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */

(function($) {

	$.fn.dynSlide = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prev',
			nextId:			'next',
			firstId:		'first',
			lastId:			'last',
			container:		'#slider ul',
			speed: 			800,
			count:			0,
			index:			0,
			step:			2,
			offset:			100
		}; 
		
		if( $.browser.msie && $.browser.version.substring(0,1) < 8 ) {
			return false;
		}
		
		
		var options = $.extend( defaults, options );  
		options.container = this;
		
		$( this ).css( 'display', 'table' );			

		this.children().each( function() {
			$( this ).css( 'display', 'table-cell' ).css( 'float', 'none' ).css( 'width', 'auto' );
			options.count += 1;
		});

		$( this ).css( 'width', (100 * options.count / options.step) + '%' );

		$( "#"+options.nextId ).click( function () {
			slide( "next" );
			return false;
		});
		$( "#"+options.prevId ).click(function(){		
			slide( "prev" );
			return false;			
		});	
		$( "#"+options.firstId ).click(function(){		
			slide( "first" );
			return false;
		});				
		$( "#"+options.lastId ).click(function(){		
			slide( "last" );
			return false;			
		});		
									
		function slide( dir ){
			switch( dir ){
				case "next":				
					options.index = ( options.index + options.step  >= options.count - options.step ) ? 
									options.count - options.step : options.index + options.step;
					break; 
				case "prev":
					options.index = ( options.index <= 0 ) ? 
									0 : (options.index - options.step < 0 ? 0 : options.index - options.step );						
					break; 
				case "first":
					options.index = 0;
					break; 
				case "last":
					options.index = options.count - options.step
					break; 
				default:
					options.index = ( options.index >= options.count ) ? options.count : options.index + options.step;						
					break; 
				};

				$( options.container ).animate(
					{ marginLeft: ( options.index * ( options.offset / options.step ) * -1 ) + "%" }, 
					{ queue: false, duration: options.speed }
				);						
		};


}})(jQuery);